const http = require('../../network/http')
const api = require('../../network/api')
// pages/hardWare/hardWare.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    storeName: '',
    storeId: '',
    roomList: [],
    aocnType: [],
    startTime: '00:00',
    endTime: '23:59',
    lampAddress: '',
    lampId: '',
  },
  getRoom : function(e){
    http.request({
      showLoading: true,
      url: api.hardRoom,
      method: "GET",
      data: {
        storeId: this.data.storeId,
      },
    }).then(res => {
      if (res.data.code == 200) {
        console.log("房间列表:");
        console.log(res.data.result);
        var nAocnType = [];
        for(var i=0 ; i< res.data.result.itemVOList.length ; i++){
          nAocnType.push(0);
        }
        var startTime;
        if(res.data.result.storeFasciaOpenTime == null){
          startTime = "请选择";
        }else{
          startTime = res.data.result.storeFasciaOpenTime;
        }
        var endTime;
        if(res.data.result.storeFasciaCloseTime == null){
          endTime = "请选择";
        }else{
          endTime = res.data.result.storeFasciaCloseTime;
        }
        this.setData({
          'roomList': res.data.result.itemVOList,
          'aocnType': nAocnType,
          lampAddress: res.data.result.storeFasciaAddr,
          lampId: res.data.result.storeFasciaAcon,
          startTime: startTime,
          endTime: endTime,
        })
      }else{
        wx.showToast({
          title: res.data.message,
          icon: "none",
          duration: 2000
        });
      }
    }).catch(err => {
      wx.showToast({
        title: err,
        icon: "none",
        duration: 2000
      });
    });
  },
  clickDoor : function(e){
    var index = e.currentTarget.dataset.index;
    console.log("clickDoor-------"+ JSON.stringify(e.currentTarget.dataset.bean));
    if(e.currentTarget.dataset.bean.smartLock == '' || e.currentTarget.dataset.bean.smartLock == null){
      wx.showToast({
        title: e.currentTarget.dataset.bean.itemName + "房间门锁未配置",
        icon: "none",
        duration: 2000
      });
    }else{
      http.request({
        showLoading: true,
        url: api.doorTest,
        method: "GET",
        data: {
          smartLockId: e.currentTarget.dataset.bean.smartLock,
        },
      }).then(res => {
        if (res.data.code == 200) {
          wx.showToast({
            title: e.currentTarget.dataset.bean.itemName + "门锁操作成功",
            icon: "none",
            duration: 2000
          });
        }else{
          wx.showToast({
            title: res.data.message,
            icon: "none",
            duration: 2000
          });
        }
      }).catch(err => {
        wx.showToast({
          title: err,
          icon: "none",
          duration: 2000
        });
      });
    }
    
  },
  clickSwitch : function(e){
    var index = e.currentTarget.dataset.index;
    if(e.currentTarget.dataset.bean.aocn == '' || e.currentTarget.dataset.bean.aocn == null){
      wx.showToast({
        title: e.currentTarget.dataset.bean.itemName + "房间空开未配置",
        icon: "none",
        duration: 2000
      });
    }else{
      var operation = 0;
      var nAocnType = this.data.aocnType;
      if(this.data.aocnType[index] == 0){
        operation = 1;
        nAocnType[index] = 1;
      }else if(this.data.aocnType[index] == 1){
        operation = 0;
        nAocnType[index] = 0;
      }
      http.request({
        showLoading: true,
        url: api.switchTest,
        method: "GET",
        data: {
          operation: operation,
          pathAddress: e.currentTarget.dataset.bean.aocnPathAddress,
          smartAocnId: e.currentTarget.dataset.bean.aocn,
        },
      }).then(res => {
        if (res.data.code == 200) {
          this.setData({
            'aocnType': nAocnType,
          })
          wx.showToast({
            title: e.currentTarget.dataset.bean.itemName + "空开操作成功",
            icon: "none",
            duration: 2000
          });
        }else{
          wx.showToast({
            title: res.data.message,
            icon: "none",
            duration: 2000
          });
        }
      }).catch(err => {
        wx.showToast({
          title: err,
          icon: "none",
          duration: 2000
        });
      });
    }
    
  },
  clickAudio : function(e){
    if(e.currentTarget.dataset.bean.smartAudio == '' || e.currentTarget.dataset.bean.smartAudio == null){
      wx.showToast({
        title: e.currentTarget.dataset.bean.itemName + "房间音响未配置",
        icon: "none",
        duration: 2000
      });
    }else{
      http.request({
        showLoading: true,
        url: api.audioTest,
        method: "GET",
        data: {
          smartAudioId: e.currentTarget.dataset.bean.smartAudio,
        },
      }).then(res => {
        if (res.data.code == 200) {
          wx.showToast({
            title: e.currentTarget.dataset.bean.itemName + "音响操作成功",
            icon: "none",
            duration: 2000
          });
        }else{
          wx.showToast({
            title: res.data.message,
            icon: "none",
            duration: 2000
          });
        }
      }).catch(err => {
        wx.showToast({
          title: err,
          icon: "none",
          duration: 2000
        });
      });
    }
    
  },
  chooseStart : function(e){
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      startTime: e.detail.value
    })
  },
  chooseEnd : function(e){
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      endTime: e.detail.value
    })
  },
  submitTime : function(e){
    if(this.data.startTime == "请选择" || this.data.endTime == "请选择"){
      wx.showToast({
        title: '请先选择时间',
        icon: "none",
        duration: 2000
      });
      return;
    }
    if(this.data.startTime == this.data.endTime){
      wx.showToast({
        title: '开关时间不能相同',
        icon: "none",
        duration: 2000
      });
      return;
    }
    http.request({
      showLoading: true,
      shake: true,
      url: api.editTime,
      method: 'POST',
      data: {
        storeId: this.data.storeId,
        storeFasciaOpenTime: this.data.startTime,
        storeFasciaCloseTime: this.data.endTime,
      },
    }).then(res => {
      if (res.data.code == 200) {
        setTimeout(function () {
          wx.showToast({
            title: '操作成功',
            icon: "none",
            duration: 2000
          });
        }, 1000);
      } else {
        setTimeout(function () {
          wx.showToast({
            title: res.data.message,
            icon: "none",
            duration: 2000
          });
        }, 1000);
      }
    }).catch(err => {
      setTimeout(function () {
        wx.showToast({
          title: err,
          icon: "none",
          duration: 2000
        });
      }, 1000);
    });
  },
  openLamp : function(e){
    http.request({
      showLoading: true,
      url: api.switchTest,
      method: "GET",
      data: {
        operation: 1,
        pathAddress: this.data.lampAddress,
        smartAocnId: this.data.lampId,
      },
    }).then(res => {
      if (res.data.code == 200) {
        wx.showToast({
          title: "操作成功",
          icon: "none",
          duration: 2000
        });
      }else{
        wx.showToast({
          title: res.data.message,
          icon: "none",
          duration: 2000
        });
      }
    }).catch(err => {
      wx.showToast({
        title: err,
        icon: "none",
        duration: 2000
      });
    });
  },
  closeLamp : function(e){
    http.request({
      showLoading: true,
      url: api.switchTest,
      method: "GET",
      data: {
        operation: 0,
        pathAddress: this.data.lampAddress,
        smartAocnId: this.data.lampId,
      },
    }).then(res => {
      if (res.data.code == 200) {
        wx.showToast({
          title: "操作成功",
          icon: "none",
          duration: 2000
        });
      }else{
        wx.showToast({
          title: res.data.message,
          icon: "none",
          duration: 2000
        });
      }
    }).catch(err => {
      wx.showToast({
        title: err,
        icon: "none",
        duration: 2000
      });
    });
  },
  closeview : function(e){
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      storeName: options.storeName,
      storeId: options.storeId,
    })
    this.getRoom();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})